<?php
    include('../configs/connect.php');
//************************************************************************************************************************************     
    session_start();
    if(isset($_POST['login'])){
        $arr_err=array();
        $arr_field=array('id_DN'=>'Tài khoản','MatKhau_DN'=>'Mật khẩu');
        foreach($arr_field as $key=>$values){
            if(!isset($_POST[$key]) || empty($_POST[$key])){
                $arr_err[]="Trường <font color=red> {$values} </font>chưa nhập";       
            }            
        }  //end foreach
        $id_DN=$_POST['id_DN'];
        $MatKhau_DN=$_POST['MatKhau_DN'];   
        
        if(empty($arr_err)){
               
            $MatKhau_sha1=sha1($MatKhau_DN);            
            $sql_check="SELECT * FROM tbl_dangnhap WHERE matkhau_dn='{$MatKhau_sha1}' and ten_dangnhap='{$id_DN}'";
            $ex=mysql_query($sql_check);
            if(mysql_num_rows($ex)==1){
                while($rows =  mysql_fetch_array($ex,MYSQL_ASSOC)){
                    $tendn=$rows['ten_dangnhap'];
                    $ma_sv=$rows['ma_sv'];
                    if($ma_sv==""){
                        $ex_macb=mysql_query("SELECT 	tbl_dangnhap.ma_cb,tbl_dangnhap.ma_pq,hoten_cb,tbl_phanquyen.ma_pq,ten_pq 
                                                FROM 	tbl_dangnhap,tbl_canbo,tbl_phanquyen
                                                WHERE 	tbl_dangnhap.ma_pq=tbl_phanquyen.ma_pq
                                                AND 	tbl_dangnhap.ma_cb=tbl_canbo.ma_cb 
                                                AND     ten_dangnhap='{$tendn}'");
                            while($rows=mysql_fetch_array($ex_macb)){
                                $_SESSION['ma_cb']=$rows['ma_cb'];
                                $_SESSION['ma_pq']=$rows['ma_pq'];
                                $_SESSION['ten_dn']=$rows['hoten_cb'];
                                $_SESSION['ten_pq']=$rows['ten_pq'];                                    
                            }
                    }else{
                        $ex_macb=mysql_query("  SELECT 	tbl_dangnhap.ma_sv,hoten_sv,tbl_phanquyen.ma_pq,ten_pq 
                                                FROM 	tbl_dangnhap,tbl_sinhvien,tbl_phanquyen
                                                WHERE 	tbl_dangnhap.ma_pq=tbl_phanquyen.ma_pq
                                                AND 	tbl_dangnhap.ma_sv=tbl_sinhvien.ma_sv 
                                                AND     ten_dangnhap='{$tendn}'");
                            while($rows=mysql_fetch_array($ex_macb)){
                                $_SESSION['ma_cb']=$rows['ma_sv'];
                                $_SESSION['ma_pq']=$rows['ma_pq'];
                                $_SESSION['ten_dn']=$rows['hoten_sv'];
                                $_SESSION['ten_pq']=$rows['ten_pq'];                                    
                            }
                    }
                        
                    header('Location: index.php');
                }//end while
            }else {
                $arr_err[]="<b> Nhập sai Tài khoản hoặc Mật khẩu</b>";
            }            
        }  
    }//end if(isert))
        
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Đăng Nhập</title>
	<link type="text/css" rel="stylesheet" href="../php/css/invalid.css" />    
    <link type="text/css" rel="stylesheet" href="../php/css/reset.css" />
    <link type="text/css" rel="stylesheet" href="../php/css/style.css" />
    <link type="text/css" rel="stylesheet" href="../php/css/travel.css" />
    
    <script type="text/javascript" src="../php/js/jquery144.js"></script>
    <script type="text/javascript" src="../php/js/simpla.jquery.configuration.js"></script>
    <script type="text/javascript" src="../php/js/date/jquery.js"></script>
    <script type="text/javascript" src="../php/js/date/jquery-calendar.js"></script>
    <link type="text/css" rel="stylesheet" href="../php/js/date/jquery-calendar.css" />   
    
</head>
  
	<body id="login">
		
		<div id="login-wrapper" class="png_bg">
			<div id="login-top">
			<h2><font color=#ffffff>Hệ Thống Hỗ Trợ Quản Lý Đào Tạo Từ Xa <br /> Tại Viện Đại Học Mở Hà Nội</font> </h2> 
			</div> <!-- End #logn-top -->
			
			<div id="login-content">
				<br />				<br />				<br />				<br />
        <form action="" method="post">
        
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <a href="bosungtt_sv.php" style="color: #f342f7;"><b></b></a>
                    <br /><br />
                    <p>
						<label>Tài Khoản: </label>
						<input class="text-input"  name="id_DN" type="text" autocomplete="off" />
					</p>
					<div class="clear"></div>
					<p>
						<label>Mật Khẩu: </label>
						<input class="text-input"  name="MatKhau_DN" type="password" value="123456"/>
					</p>
					<div class="clear"></div>
					<p>
						<input class="button" name="login" type="submit" value="Sign In" />
					</p>
                    
					
		</form>
    
<?php
    if(!empty($arr_err)){
        echo"<ul>";
        foreach($arr_err as $err){
            echo "<li> $err </li>";
        }
        echo"</ul>"; 
        
    }
     
?>    
			</div> <!-- End #login-content -->
			
		</div> <!-- End #login-wrapper -->
  </body>
</html>
